<?php

namespace app\admin\validate;

use think\Db;
use think\Validate;

class Buser extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'username' => 'require|max:32|unique:buser',
      /*  'qq'       => 'unique:buser',
        'wechat'   => 'unique:buser',
        'mobile'   => 'unique:buser',
        'email'    => 'email|unique:buser'*/
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'username.require' => '名称必须',
        'username.max' => '名称长度超过限度',
        'username.unique'  => '客户名称已被占用',
        'qq.unique'        => 'QQ已被占用',
        'wechat.unique'    => '微信已被占用',
        'mobile.unique'    =>'手机已被占用',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => ['username'=>'require|max:32|checkHas','mobile'=>'checkHas'],
    ];

    /**
     * 自定义修改验证是否已存在
     */
    public function checkHas($value,$rule,$data,$field)
    {
        $post=input();
        $find = Db::name('buser')->where($field,$value)->find();
        if($find && $find['id']!=$post['ids'])
            return $field.'已被占用';
        else
        {
            return true;
        }
    }
}
